package com.srwl.mytx.contrack;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.srwl.mytx.Helper;
import com.srwl.mytx.R;
import com.srwl.mytx.RetrofitManager;
import com.srwl.mytx.activity.BaseActivity;
import com.srwl.mytx.domain.CommonResult;
import com.srwl.mytx.domain.Topic;
import com.srwl.mytx.service.TopicService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.srwl.mytx.contrack.GoodsConstrack.GOODS_STATUS_BACKOUT;
import static com.srwl.mytx.contrack.GoodsConstrack.GOODS_STATUS_NORMAL;

public class GoodsPresenterImpl implements GoodsConstrack.Presenter {
    private static final String TAG = "GoodsPresenterImpl";
    private GoodsConstrack.View view;
    private Context context;

    public GoodsPresenterImpl(GoodsConstrack.View view, Context context) {
        this.view = view;
        this.context = context;
    }

    @Override
    public void backOut(final Topic targetGoods, String goodsId) {
        if (!Helper.getInstance().isNetworkConnected()) {
            Toast.makeText(context, context.getString(R.string.network_anomalies), Toast.LENGTH_SHORT).show();
            return;
        }
        Topic updateGoods = new Topic();
        updateGoods.setStatus(GOODS_STATUS_BACKOUT);
        Map<String, Object> info = new HashMap<>();
        info.put("id", goodsId);
        info.put("blog", updateGoods);
        RetrofitManager.getInstance().createRequest(TopicService.class).update(info)
                .enqueue(new Callback<CommonResult>() {
                    @Override
                    public void onResponse(Call<CommonResult> call, Response<CommonResult> response) {
                        view.disMissProgressDialog();
                        CommonResult result = response.body();
                        if (!response.isSuccessful() || result == null) {
                            view.showToast(context.getString(R.string.system_busy));
                            return;
                        }
                        if (result.isError()) {
                            Log.e(TAG, result.getMsg());
                            view.showToast(context.getString(R.string.system_busy));
                            return;
                        }
                        view.onBackOutComplete(targetGoods);

                    }

                    @Override
                    public void onFailure(Call<CommonResult> call, Throwable t) {
                        t.printStackTrace();
                        Log.e(TAG, t.getMessage());
                        view.disMissProgressDialog();
                        view.showToast(context.getString(R.string.system_busy));
                    }
                });
    }

    @Override
    public void onLine(final Topic targetGoods, String goodsId) {
        if (!Helper.getInstance().isNetworkConnected()) {
            Toast.makeText(context, context.getString(R.string.network_anomalies), Toast.LENGTH_SHORT).show();
            return;
        }
        Topic updateGoods = new Topic();
        updateGoods.setStatus(GOODS_STATUS_NORMAL);
        updateGoods.setUpdated(new Date());
        Map<String, Object> info = new HashMap<>();
        info.put("id", goodsId);
        info.put("blog", updateGoods);
        RetrofitManager.getInstance().createRequest(TopicService.class).update(info)
                .enqueue(new Callback<CommonResult>() {
                    @Override
                    public void onResponse(Call<CommonResult> call, Response<CommonResult> response) {
                        view.disMissProgressDialog();
                        CommonResult result = response.body();
                        if (!response.isSuccessful() || result == null) {
                            Log.e(TAG, "onResponse: " + response.message());
                            view.showToast(context.getString(R.string.system_busy));
                            return;
                        }
                        if (result.isError()) {
                            Log.e(TAG, result.getMsg());
                            view.showToast(context.getString(R.string.system_busy));
                            return;
                        }
                        view.onSetOnLineComplete(targetGoods);
                    }

                    @Override
                    public void onFailure(Call<CommonResult> call, Throwable t) {
                        t.printStackTrace();
                        Log.e(TAG, t.getMessage());
                        view.disMissProgressDialog();
                        view.showToast(context.getString(R.string.system_busy));
                    }
                });
    }

    @Override
    public void searchByUid(Map<String, Object> info, final GoodsConstrack.QueryType queryType) {
        if (!Helper.getInstance().isNetworkConnected()) {
            Toast.makeText(context, context.getString(R.string.network_anomalies), Toast.LENGTH_SHORT).show();
            return;
        }
        RetrofitManager.getInstance().createRequest(TopicService.class).searchByUid(info)
                .enqueue(new Callback<CommonResult>() {
                    @Override
                    public void onResponse(Call<CommonResult> call, Response<CommonResult> response) {
                        view.disMissProgressDialog();
                        CommonResult result = response.body();
                        if (!response.isSuccessful() || result == null) {
                            Log.e(TAG, "onResponse: " + response.message());
                            view.showToast(context.getString(R.string.system_busy));
                            return;
                        }
                        if (result.isError()) {
                            Log.e(TAG, result.getMsg());
                            view.showToast(context.getString(R.string.system_busy));
                            return;
                        }


                        JSONObject jsonObject = (JSONObject) result.getData();
                        List<Topic> goods = JSONObject.parseArray(jsonObject.getString("hits"), Topic.class);
                        Integer total = jsonObject.getInteger("total");
                        view.notifyDateChange(goods, queryType, total);
                        Log.d(TAG, jsonObject.toJSONString());
                    }

                    @Override
                    public void onFailure(Call<CommonResult> call, Throwable t) {
                        view.disMissProgressDialog();
                        view.showToast(context.getString(R.string.system_busy));
                        t.printStackTrace();
                        Log.e(TAG, t.getMessage());
                    }
                });
    }

    @Override
    public void refresh(final Topic targetGoods, String goodsId) {
        if (!Helper.getInstance().isNetworkConnected()) {
            ((BaseActivity) context).showToast_v1(context.getString(R.string.network_anomalies));
            return;
        }
        Topic updateGoods = new Topic();
        updateGoods.setUpdated(new Date());
        Map<String, Object> info = new HashMap<>();
        info.put("id", goodsId);
        info.put("blog", updateGoods);
        RetrofitManager.getInstance().createRequest(TopicService.class).update(info)
                .enqueue(new Callback<CommonResult>() {
                    @Override
                    public void onResponse(Call<CommonResult> call, Response<CommonResult> response) {
                        view.disMissProgressDialog();
                        CommonResult result = response.body();
                        if (!response.isSuccessful() || result == null) {
                            Log.e(TAG, "onResponse: " + response.message());
                            view.showToast(context.getString(R.string.system_busy));
                            return;
                        }
                        if (result.isError()) {
                            Log.e(TAG, result.getMsg());
                            view.showToast(context.getString(R.string.system_busy));
                            return;
                        }
                        view.onRefreshComplete(targetGoods);
                    }

                    @Override
                    public void onFailure(Call<CommonResult> call, Throwable t) {
                        t.printStackTrace();
                        Log.e(TAG, t.getMessage());
                        view.disMissProgressDialog();
                        view.showToast(context.getString(R.string.system_busy));
                    }
                });
    }

    @Override
    public void update(final Topic targetGoods, String goodsId) {
        if (!Helper.getInstance().isNetworkConnected()) {
            ((BaseActivity) context).showToast_v1(context.getString(R.string.network_anomalies));
            return;
        }
        Map<String, Object> info = new HashMap<>();
        info.put("id", goodsId);
        info.put("blog", targetGoods);
        RetrofitManager.getInstance().createRequest(TopicService.class).update(info)
                .enqueue(new Callback<CommonResult>() {
                    @Override
                    public void onResponse(Call<CommonResult> call, Response<CommonResult> response) {
                        view.disMissProgressDialog();
                        CommonResult result = response.body();
                        if (!response.isSuccessful() || result == null) {
                            Log.e(TAG, "onResponse: " + response.message());
                            view.showToast(context.getString(R.string.system_busy));
                            return;
                        }
                        if (result.isError()) {
                            Log.e(TAG, result.getMsg());
                            view.showToast(context.getString(R.string.system_busy));
                            return;
                        }
                        view.onUpdateComplete(targetGoods);
                    }

                    @Override
                    public void onFailure(Call<CommonResult> call, Throwable t) {
                        t.printStackTrace();
                        Log.e(TAG, t.getMessage());
                        view.disMissProgressDialog();
                        view.showToast(context.getString(R.string.system_busy));
                    }
                });
    }

    @Override
    public void delete(final Topic targetGoods, String goodsId) {
        if (!Helper.getInstance().isNetworkConnected()) {
            Toast.makeText(context, context.getString(R.string.network_anomalies), Toast.LENGTH_SHORT).show();
            return;
        }
        RetrofitManager.getInstance().createRequest(TopicService.class).delete(goodsId)
                .enqueue(new Callback<CommonResult>() {
                    @Override
                    public void onResponse(Call<CommonResult> call, Response<CommonResult> response) {
                        view.disMissProgressDialog();
                        CommonResult result = response.body();
                        if (!response.isSuccessful() || result == null) {
                            Log.e(TAG, "onResponse: " + response.message());
                            view.showToast(context.getString(R.string.system_busy));
                            return;
                        }
                        if (result.isError()) {
                            Log.e(TAG, result.getMsg());
                            view.showToast(context.getString(R.string.system_busy));
                            return;
                        }
                        view.onDeleteComplete(targetGoods);


                    }

                    @Override
                    public void onFailure(Call<CommonResult> call, Throwable t) {
                        t.printStackTrace();
                        Log.e("SellingFragment", t.getMessage());
                        view.disMissProgressDialog();
                        view.showToast(context.getString(R.string.system_busy));

                    }
                });
    }

    @Override
    public void start() {

    }

}
